package com.erp.dao;

import com.erp.bean.User;
import com.erp.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    JDBCUtils jdbcUtils = new JDBCUtils();

    public List<User> listQuery(int page,int size){
        List<User>list = new ArrayList<>();

        try {
            PreparedStatement pstm = null;
            ResultSet rs = null;
            Connection connection = jdbcUtils.getConnection();
            if (connection != null) {
                String sql = "select * from user limit ?,?";
                Object[] params = {page, size};
                rs = jdbcUtils.execute(connection, sql, params, rs, pstm);
                while (rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setSex(rs.getString("sex"));
                    user.setEmail(rs.getString("email"));
                    user.setPhone(rs.getString("phone"));
                    list.add(user);
                }
                jdbcUtils.closeResource(connection,pstm,rs);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }
}
